function [data,numsubjects,valid_ids] = parseoutput(filename,configfile,q)

    % This function parses the data output file and produces a data matrix
    % with the relevant information.
    % It also returns the number of trials read.
    % It is common to both treatments
    
    %%%%%%%%%%%%%%
    % read in data
    %%%%%%%%%%%%%%

    fileID = fopen(filename,'r');
    keepreading = 1; % when not end of file
    
    row = 1;
    valid_ids = [];
    while keepreading
        % read in a line
        line = textscan(fileID, '%s %s %s %s %s %f %s %s %s %s %f %s %f %s %s %f %f %f %f %f %f %f %s %s %s %f %q %s %s %f %s %f %f %f %s %f %f %f %f %f %f %f %f %f %f %f %f',1,'HeaderLines',1,'Delimiter',',');
        % check if we've reached the end of the file
        if isempty(line{1})
            keepreading = 0;
        else
            % extract the values we need, converting to numeric
            subjectid(row) = double(line{16});
            payoff(row) = double(line{19});
            switch char(line{23})
                case 'Buy'
                    realizedtrade(row) = 1;
                case 'Sell'
                    realizedtrade(row) = -1;
                case 'No Trade'
                    realizedtrade(row) = 0;
                otherwise
                    realizedtrade(row) = NaN;
            end;
            switch char(line{24})
                case 'Buy'
                    bluetrade(row) = 1;
                case 'Sell'
                    bluetrade(row) = -1;
                case 'No Trade'
                    bluetrade(row) = 0;
                otherwise
                    bluetrade(row) = NaN;
            end;
            switch char(line{25})
                case 'Buy'
                    greentrade(row) = 1;
                case 'Sell'
                    greentrade(row) = -1;
                case 'No Trade'
                    greentrade(row) = 0;
                otherwise
                    greentrade(row) = NaN;
            end;
            currentrow(row) = double(line{26}); % row in config file that applies to this round
            switch char(line{29})
                case 'Freshman'
                    education(row) = 0;
                case 'Sophomore'
                    education(row) = 1;
                case 'Junior'
                    education(row) = 2;
                case 'Senior'
                    education(row) = 3;
                otherwise
                    education(row) = NaN;
            end;
            age(row) = double(line{30});
            switch char(line{31})
                case 'Male'
                    gender(row) = 1;
                case 'Female'
                    gender(row) = 0;
                otherwise
                    gender(row) = NaN;
            end;
            
            round(row) = double(line{34});
            
            % use age being recorded as indication this player finished so
            % capture his id
            if age(row) >= 13  % 13 is minimum age
                valid_ids = [valid_ids subjectid(row)];
            end;
        end;
        
        row = row+1;
    end;
    fclose(fileID);
    
    % put all data in one matrix
    tempdata = [subjectid' round' currentrow' payoff' realizedtrade' bluetrade' greentrade' education' age' gender'];
    

    % simple check on whether or not file was read correctly
%     disp('Subjects found:');
%     valid_ids
    numsubjects = size(valid_ids,2);
    
    % filter data to include only those valid subjects' data
    data = [];
    for i=1:numsubjects
        data = [data;  tempdata(tempdata(:,1)==valid_ids(i),:)];
    end
    
    % sort by subject and then currentrow
    data = sortrows(data,[1 3]);
    
    % read in config
    fileID = fopen(configfile,'r');
    keepreading = 1; % when not end of file
    
    row = 1;
    while keepreading
        % read in a line
        line = textscan(fileID, '%f %f %f %q %f %f %f %f %f',1,'HeaderLines',1,'Delimiter',',');
        % check if we've reached the end of the file
        if isempty(line{1})
            keepreading = 0;
        else
            % extract the values we need, converting to numeric
            turn(row) = double(line{3});
            assetval(row) = double(line{5});
            signals{row} = char(line{4});
            opposite(row) = double(line{9});
        end;
        
        row = row+1;
    end;
    fclose(fileID);
    
    % because data is sorted, can directly add copies of config (one per
    % subject)
   
    data = [data repmat(turn',numsubjects,1)];
    data = [data repmat(assetval',numsubjects,1)];
    
    % process signals and add to data
    signals = repmat(signals',numsubjects,1);
    [tradeprice contradiction streak] = processsignals(signals,q);
    
    data = [data tradeprice contradiction streak];
    data = [data repmat(opposite',numsubjects,1)];
    
    % create dummies for previous asset values to test for across round
    % effects
    lastassetval = nan*ones(size(data,1),1);
    assetval11 = nan*ones(size(data,1),1); % last two asset values were 1
    assetval00 = nan*ones(size(data,1),1); % last two asset values were 0
    assetval111 = nan*ones(size(data,1),1); % last three asset values were 1
    assetval000 = nan*ones(size(data,1),1); % last three asset values were 0
    for i=1:size(data,1)
        curr_round = data(i,2);
        curr_subj = data(i,1);
        if curr_round>1
            lastroundrow = find((data(:,2)==curr_round-1)&(data(:,1)==curr_subj));
            lastassetval(i) = data(lastroundrow,12);
        end
        if curr_round>2
            lastroundrow = find((data(:,2)==curr_round-1)&(data(:,1)==curr_subj));
            lastlastroundrow = find((data(:,2)==curr_round-2)&(data(:,1)==curr_subj));
            assetval11(i) = (data(lastroundrow,12)==1)&&(data(lastlastroundrow,12)==1);
            assetval00(i) = (data(lastroundrow,12)==0)&&(data(lastlastroundrow,12)==0);
        end
        if curr_round>3
            lastroundrow = find((data(:,2)==curr_round-1)&(data(:,1)==curr_subj));
            lastlastroundrow = find((data(:,2)==curr_round-2)&(data(:,1)==curr_subj));
            lastlastlastroundrow = find((data(:,2)==curr_round-3)&(data(:,1)==curr_subj));
            assetval111(i) = (data(lastroundrow,12)==1)&&(data(lastlastroundrow,12)==1)&&(data(lastlastlastroundrow,12)==1);
            assetval000(i) = (data(lastroundrow,12)==0)&&(data(lastlastroundrow,12)==0)&&(data(lastlastlastroundrow,12)==0);
        end
    end
    
    data = [data lastassetval assetval11 assetval00 assetval111 assetval000];